package adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.drangh.MainActivity;
import com.example.drangh.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.zip.Inflater;

import bean.HomeBean;

public class RlvAdapter extends RecyclerView.Adapter<RlvAdapter.ViewHolder> {
    public boolean b;
    private List<HomeBean.ResultsBean> beans = new ArrayList<>();
    private Context context;

    public RlvAdapter(List<HomeBean.ResultsBean> beans) {
        this.beans = beans;
    }
  public void   getData(List<HomeBean.ResultsBean>beans){
        this.beans.addAll(beans);
        notifyDataSetChanged();
  }

    @NonNull
    @Override
    public RlvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        Context context = null;
        LayoutInflater.from(context).inflate(R.layout.item,viewGroup,false);
        View inflater = null;
        return new ViewHolder(inflater);
    }

    @Override
    public void onBindViewHolder(@NonNull RlvAdapter.ViewHolder viewHolder, int i) {
       HomeBean.ResultsBean resultsBean = beans.get(i);
        ViewGroup.LayoutParams layoutParams = viewHolder.itemView.getLayoutParams();
        boolean b = false;
        if (b){
            float v = Dp.dpToPx(this.context, 400);
            layoutParams.height= (int) v;
      }else{
           int a = new Random().nextInt(800-300+1)+300;
          float v = DpUtis.dpTopx(this.context,a);
          layoutParams.height =(int) v;
        }
        Glide.with(context).load(resultsBean.getUrl()).into(viewHolder.mIvImg);
    }

    @Override
    public int getItemCount() {
        return beans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

       public ImageView mIvimg;
        private final ImageView mIvImg;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mIvImg = itemView.findViewById(R.id.img);
        }
    }
}
